package com.huike.game.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.huike.game.Bean.Bean22;
import com.huike.game.R;

import java.util.ArrayList;

public class MyAdpter extends BaseAdapter {

    ArrayList<Bean22.DataBean> data = null;
    RequestQueue queue =null;
    Context context=null;
    String TAG ="log";
    //初始化数据
    public MyAdpter(ArrayList<Bean22.DataBean> a, Context context){
        data=a;
        this.context=context;//初始化上下文
        this.queue= Volley.newRequestQueue(context);//初始化消息队列
    }

    //返回多少条数据
    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //获取对应数据
        Bean22.DataBean dataBean = data.get(position);
        //创建一个视图绑定控件的类
        ViewCache viewCache = null;

        if(convertView ==null){
            //加载静态布局
            convertView = View.inflate(parent.getContext(), R.layout.item22, null);
            //绑定控件
            viewCache=new ViewCache(convertView);
            //设置缓存
            convertView.setTag(viewCache);
        }else {
            //获取缓存
            viewCache = (ViewCache)convertView.getTag();
        }
        //未控件设置值
        viewCache.textView.setText(dataBean.getCarName());
        //        final ViewCache finalViewCache = viewCache;
        final ViewCache finalViewCache = viewCache;
        ImageRequest imageRequest = new ImageRequest(
                dataBean.getImage(),
                new Response.Listener<Bitmap>() {
                    @Override
                    public void onResponse(Bitmap bitmap) {
                        finalViewCache.imageView.setImageBitmap(bitmap);
                    }
                },
                200,
                200,
                ImageView.ScaleType.FIT_XY,
                Bitmap.Config.ARGB_8888,
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.e(TAG, "onErrorResponse: "+volleyError.getMessage());
                    }
                }
        );

        queue.add(imageRequest);
        Log.e("log", "getView: "+dataBean.getCarName() );
        return convertView;
    }

    static class ViewCache{
        ImageView imageView;
        TextView textView;
        ViewCache(View view){
             imageView = view.findViewById(R.id.image_v22);
             textView = view.findViewById(R.id.tv1);
        }
    }

}
